home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 403_01 / matrix.doc < prev    next >
Encoding:
Text File  |  1993-09-22  |  17.9 KB  |  482 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *       file:   matrix.doc
  4. *       desc:   document for matrix toolbox function calls
  5. *       by:     ko shu pui, patrick
  6. *       date:   24 may 92       v0.4
  7. *               23 sep 93       v0.41
  8. *-----------------------------------------------------------------------------
  9. */
  10.  
  11. ===============================================================================
  12. 0. INTRODUCTION
  13. ===============================================================================
  14. This document only provides you the following information:
  15.  
  16.         0.1     How to create and free a matrix
  17.         0.2     Description of each matrix function call
  18.         0.3     Some hints to use this toolbox
  19.  
  20. Remember that this document will NOT describe the data structure and
  21. any technical details of the toolbox - just because this document is
  22. aimed at to be a sort-of "User's Guide" for programmers.
  23.  
  24.  
  25.  
  26.  
  27. ===============================================================================
  28. 1. OUR MATRIX OF REFERENCE
  29. ===============================================================================
  30. In order to avoid terms confusion, here is our matrix of reference
  31. (matrix A) which is of size m x n.
  32.  
  33.                           Column
  34.                 Row        0       1       2          n-1
  35.                  0    [   a0,0    a0,1    a0,2   ...  a0,n-1   ]
  36.      A =         1    [   a1,0    a1,1    a1,2   ...  a1,n-1   ]
  37.                  2    [   a2,0    a2,1    a2,2   ...  a2,n-1   ]
  38.                       [   ...     ...     ...    ...  ...      ]
  39.                  m-1  [   am-1,0  am-1,1  am-1,2 ...  am-1,n-1 ]
  40.  
  41.  
  42.  
  43. ===============================================================================
  44. 2. BASIC MATRIX OBJECT OPERATION
  45. ===============================================================================
  46. -------------------------------------------------------------------------------
  47. Function :      MATRIX mat_creat (int m, int n, int type)
  48. Synopsis :      creation of a matrix which can be used by the matrix toolbox
  49.                 functions; memory is allocated for this object; and some
  50.                 initialization is performed.
  51. Parameter:      m: number of rows
  52.                 n: number of columns
  53.                 type: matrix initialization type where
  54.  
  55.                 type=
  56.  
  57.                 UNDEFINED       do not initialize the matrix
  58.                 ZERO_MATRIX     fill zero to all matrix elements
  59.                 UNIT_MATRIX     fill a one to all matrix element ai,j
  60.                                 where i=j
  61.  
  62. Return Value:   the matrix object
  63. Example:
  64.  
  65.                 MATRIX  A;
  66.  
  67.                 /*
  68.                 * create a 15 x 15 matrix;
  69.                 * do not initialize the elements
  70.                 */
  71.                 A = mat_creat( 15, 15, UNDEFINED);
  72.  
  73.                 /*
  74.                 * put a 4 in element A(0,14) of matrix A,
  75.                 * put a 2 in element A(3,5) of matrix A
  76.                 */
  77.                 A[0][14] = 4.0;
  78.                 A[3][5] = 2.0;
  79.  
  80. See Also:       mat_free(), for Accessing a matrix, see this example.
  81. -------------------------------------------------------------------------------
  82. Function:       MATRIX mat_fill ( MATRIX A, int type )
  83. Synopsis:       initialize a matrix will a simple type
  84. Parameter:      A: the matrix object for which mat_creat() has been called
  85.                 type: matrix initialization type where
  86.  
  87.                 type=
  88.  
  89.                 UNDEFINED       do not initialize the matrix
  90.                 ZERO_MATRIX     fill zero to all matrix elements
  91.                 UNIT_MATRIX     fill a one to all matrix element ai,j
  92.                                 where i=j
  93. Return Value:   MATRIX A
  94. Example:
  95.  
  96.                 MATRIX  A;
  97.  
  98.                 ...
  99.                 /*
  100.                 * fill the matrix A will zeroes
  101.                 */
  102.                 mat_fill( A, ZERO_MATRIX );
  103.  
  104. See Also:       mat_creat()
  105. -------------------------------------------------------------------------------
  106. Function :      int mat_free ( MATRIX A )
  107. Synopsis :      free all memory occupied by the matrix object A
  108. Parameter:      A: the matrix object for which mat_creat() was called
  109. Return Value:   None
  110. Example:
  111.  
  112.                 MATRIX  A;
  113.  
  114.                 A = mat_creat(...);
  115.                 ...
  116.                 mat_free(A);
  117.  
  118. Note:           since memory may be a very scarce resource in a computer,
  119.                 as a C programmer you are advised to free up the matrix as
  120.                 soon as that matrix will not be used any more in future.
  121.  
  122. See Also:       mat_creat()
  123. -------------------------------------------------------------------------------
  124. Function:       MatCol ( A )
  125. Synopsis:       find out the number of columns of a matrix object A
  126. Parameter:      A: the matrix object for which mat_creat() was called
  127. Return Value:   number of columns
  128. Example:
  129.                 int     i;
  130.  
  131.                 ...
  132.                 i = MatCol(A);
  133.  
  134. Note:           this is a macro
  135. See Also:       MatRow()
  136. -------------------------------------------------------------------------------
  137. Function:       MatRow ( A )
  138. Synopsis:       find out the number of rows of a matrix object A
  139. Parameter:      A: the matrix object for which mat_creat() was called
  140. Return Value:   number of rows
  141. Example:
  142.                 int     i;
  143.  
  144.                 ...
  145.                 i = MatRow(A);
  146.  
  147. Note:           this is a macro
  148. See Also:       MatCol()
  149.  
  150. -------------------------------------------------------------------------------
  151. Function:       MATRIX mat_colcopy1 ( MATRIX A, MATRIX B, int j1, int j2 )
  152. Synopsis:       copy a column from a matrix A to a column at matrix B
  153. Parameter:      A, B: the matrix objects for which mat_creat() was called
  154.                 column j1 of A is copied to column j2 of B;
  155. Return Value:   MATRIX A
  156. Example:
  157.                 MATRIX  A, B;
  158.  
  159.                 A = mat_creat( 5, 4, ZERO_MATRIX );
  160.                 B = mat_creat( 5, 4, UNDEFINED );
  161.  
  162.                 /*
  163.                 * copy column 2 of A to column 0 of B
  164.                 */
  165.                 mat_colcopy1( A, 2, B, 0 );
  166.  
  167. Note:           the sizes of rows of A, B must be the same
  168. See Also:       mat_copy()
  169. -------------------------------------------------------------------------------
  170. Function:       MATRIX mat_copy ( MATRIX A )
  171. Synopsis:       duplicate a matrix
  172. Parameter:      A: the matrix object for which mat_creat() was called
  173. Return Value:   Another allocated matrix object whose contents are same
  174.                 as A
  175. Example:
  176.                 MATRIX  A, B;
  177.  
  178.                 A = mat_creat( 5, 4, ZERO_MATRIX );
  179.  
  180.                 /*
  181.                 * B is also a 5 x 4 zero matrix now
  182.                 */
  183.                 B = mat_copy( A );
  184.                 ...
  185.                 mat_free( B );
  186.  
  187. See Also:       mat_colcopy1()
  188. -------------------------------------------------------------------------------
  189.  
  190.  
  191.  
  192.  
  193. ===============================================================================
  194. 3. BASIC MATRIX OBJECT INPUT/OUTPUT OPERATION
  195. ===============================================================================
  196. -------------------------------------------------------------------------------
  197. Function:       int fgetmat (MATRIX A, FILE * fp)
  198. Synopsis:       read a matrix from stream
  199. Parameter:      A: allocated matrix
  200.                 fp: a stream pointer obtained from fopen() or predefined
  201.                 pointer in standard library such as stdin
  202. Return Value:   number of matrix elements read
  203. Example:
  204.                 MATRIX  A;
  205.                 FILE    *fp;
  206.  
  207.                 A = mat_creat(3, 3, UNDEFINED);
  208.                 fp = fopen("mymatrix.dat", "r");
  209.                 fgetmat( A, fp );
  210.  
  211. See Also:       mat_dumpf(), mat_dump(), mat_fdump(), mat_fdumpf()
  212. -------------------------------------------------------------------------------
  213. Function:       MATRIX mat_dump   (MATRIX A)
  214.                 MATRIX mat_dumpf  (MATRIX A, char * format)
  215.                 MATRIX mat_fdump  (MATRIX A, FILE * fp)
  216.                 MATRIX mat_fdumpf (MATRIX A, char * format, FILE * fp)
  217.  
  218. Synopsis:       mat_dump:  dump a matrix to std output with default format
  219.                 mat_dumpf: dump a matrix to std output with specified format
  220.                 mat_fdump: dump a matrix to a file with default format
  221.                 mat_fdumpf:dump a matrix to a file with specified format
  222.  
  223. Parameter:      A: allocated matrix
  224.                 format: same as printf() 's format parameter, but can only
  225.                         be floating point type, such as "%.2f ", etc.
  226.                 fp: file pointer obtained via fopen()
  227.  
  228. Return Value:   matrix A
  229.  
  230. Example:
  231.                 MATRIX  A;
  232.  
  233.                 A = mat_creat( ... );
  234.                 ...
  235.                 /*
  236.                 * dump the matrix to standard output and each element
  237.                 * is restricted to 2 precision format
  238.                 */
  239.                 mat_dumpf( A, "%.2f ");
  240.  
  241. See Also:       fgetmat()
  242. -------------------------------------------------------------------------------
  243.  
  244.  
  245.  
  246.  
  247. ===============================================================================
  248. 4. BASIC MATRIX OBJECT MATHEMATICAL OPERATION
  249. ===============================================================================
  250. -------------------------------------------------------------------------------
  251. Function:       MATRIX mat_add (MATRIX A, MATRIX B);
  252.                 MATRIX mat_sub (MATRIX A, MATRIX B);
  253. Synopsis:       mat_add: addition of 2 matrices
  254.                 mat_sub: substraction of 2 matrices
  255. Parameter:      A, B: allocated matrices
  256. Return Value:   a newly allocated matrix which is the result of the operation
  257. Example:
  258.  
  259.                 MATRIX  A, B, C;
  260.  
  261.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  262.                 B = mat_creat( 5, 5, UNIT_MATRIX );
  263.  
  264.                 C = mat_add( A, B );
  265.  
  266.                 mat_dump( C );
  267.  
  268.                 mat_free( A );
  269.                 mat_free( B );
  270.                 mat_free( C );
  271.  
  272. Note:           A, B must be of the same dimensions
  273. See Also:       mat_mul, mat_inv
  274. -------------------------------------------------------------------------------
  275. Function:       MATRIX mat_mul (MATRIX A, MATRIX B);
  276. Synopsis:       multiplication of 2 matrices
  277. Parameter:      A, B: allocated matrix
  278. Return Value:   a newly allocated matrix which is the result of the operation
  279. Example:
  280.                 MATRIX  A, B, C;
  281.  
  282.                 A = mat_creat( 5, 3, UNIT_MATRIX );
  283.                 B = mat_creat( 3, 6, UNIT_MATRIX );
  284.  
  285.                 /*
  286.                 *  C is now a 5 x 6 matrix
  287.                 */
  288.                 C = mat_add( A, B );
  289.  
  290.                 mat_dump( C );
  291.                 ...
  292.  
  293. Note:           the column dimension of A must equal row dimension of B
  294. See Also:       mat_add, mat_sub
  295. -------------------------------------------------------------------------------
  296. Function:       MATRIX mat_inv (MATRIX A)
  297. Synopsis:       find the inverse of a matrix
  298. Parameter:      A: allocated square matrix
  299. Return Value:   a newly allocated square matrix which is the inverse of A
  300. Example:
  301.                 MATRIX  A, AI;
  302.  
  303.                 /*
  304.                 * A must be a square matrix
  305.                 */
  306.                 A = mat_creat( 7, 7, UNIT_MATRIX );
  307.                 ...
  308.                 /*
  309.                 * find the inverse of A
  310.                 */
  311.                 AI = mat_inv( A );
  312.                 ...
  313.  
  314. See Also:       mat_tran, mat_add, mat_sub
  315. -------------------------------------------------------------------------------
  316. Function:       MATRIX  mat_tran( MATRIX A )
  317. Synopsis:       find the transpose of a matrix
  318. Parameter:      A: allocated matrix
  319. Return Value:   a newly allocated matrix which is the transpose of A
  320. Example:
  321.                 MATRIX  A, T;
  322.  
  323.                 A = mat_creat( 3, 5, UNDEFINED );
  324.                 ...
  325.                 T = mat_tran( A );
  326.                 ...
  327.  
  328. See Also:       mat_inv
  329. -------------------------------------------------------------------------------
  330.  
  331.  
  332.  
  333.  
  334. ===============================================================================
  335. 5. DETERMINANT OPERATIONS
  336. ===============================================================================
  337. -------------------------------------------------------------------------------
  338. Function:       MATRIX mat_submat (MATRIX A, int i, int j)
  339. Synopsis:       form a new matrix by deleting a row and a column of A
  340. Parameter:      A: allocated matrix
  341.                 i, j: row and column of A which will not appear in the
  342.                       resulting matrix
  343. Return Value:   a new matrix whose dimensions are 1 less than A
  344. Example:
  345.                 MATRIX  A, B
  346.  
  347.                 A = mat_creat( 3, 4, UNDEFINED );
  348.                 ...
  349.                 B = mat_submat( A, 2, 2 );
  350.                 /*
  351.                 *       suppose A =  [ 1 2 3 4 ]
  352.                 *                    [ 3 3 4 5 ]
  353.                 *                    [ 6 7 9 9 ]
  354.                 *
  355.                 *       then B =     [ 1 2 4 ]
  356.                 *                    [ 3 3 5 ]
  357.                 */
  358.  
  359. Note:           Do not be misled -- the contents in A will NOT be changed
  360. See Also:       mat_det, mat_cofact, mat_minor
  361. -------------------------------------------------------------------------------
  362. Function:       double mat_cofact (MATRIX A, int i, int j)
  363. Synopsis:       find out the cofactor of A(i,j)
  364. Parameter:      A: allocated square matrix
  365.                 i,j: row, column position of A for the cofactor
  366. Return Value:   the value of cofactor of A(i,j)
  367. Example:
  368.                 MATRIX  A;
  369.  
  370.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  371.                 ...
  372.                 printf( "cofactor of A(0,2) = %f\n", mat_cofact( A, 0, 2 ));
  373.  
  374. See Also:       mat_det, mat_minor
  375. -------------------------------------------------------------------------------
  376. Function:       double mat_minor (MATRIX A, int i, int j)
  377. Synopsis:       find out the minor of A(i,j)
  378. Parameter:      A: allocated square matrix
  379.                 i,j: row, column position of A for the minor
  380. Return Value:   the value of minor of A(i,j)
  381. Example:
  382.                 MATRIX  A;
  383.  
  384.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  385.                 ...
  386.                 printf( "minor of A(0,2) = %f\n", mat_minor( A, 0, 2 ));
  387.  
  388. See Also:       mat_det, mat_cofact
  389. -------------------------------------------------------------------------------
  390. Function:       double mat_det (MATRIX A)
  391. Synopsis:       find the determinant of a matrix
  392. Parameter:      A: allocated square matrix
  393. Return Value:   the determinant value of |A|
  394. Example:
  395.                 MATRIX  A;
  396.                 double  det;
  397.  
  398.                 A = mat_creat( 5, 5, UNIT_MATRIX );
  399.                 det = mat_det( A );
  400.  
  401. See Also:       mat_cofact, mat_minor, mat_submat
  402. -------------------------------------------------------------------------------
  403.  
  404.  
  405.  
  406.  
  407. ===============================================================================
  408. 6. ADVANCED MATRIX OBJECT MATHEMATICAL OPERATION
  409. ===============================================================================
  410. -------------------------------------------------------------------------------
  411. Function:       MATRIX mat_lsolve (MATRIX A, MATRIX B)
  412. Synopsis:       solve simultaneous linear equation AX = B given A, B
  413. Parameter:      A, B: allocated matrix
  414. Return Value:   newly allocated matrix X in AX = B
  415. Example:
  416.                 MATRIX  A, B, X;
  417.  
  418.                 A = mat_creat( 5, 5, UNDEFINED );
  419.                 fgetmat( A, stdin );
  420.                 ...
  421.                 B = mat_creat( 5, 1, UNDEFINED );
  422.                 fgetmat( B, stdin );
  423.                 ...
  424.                 X = mat_lsolve( A, B );
  425.                 mat_dump( X );
  426.  
  427. Note:           number of rows in A and B must be equal
  428. See Also:       mat_lsolve_durbin
  429. -------------------------------------------------------------------------------
  430. Function:       MATRIX mat_lsolve_durbin (MATRIX A, MATRIX B)
  431. Synopsis:       simultaneous linear equations wtih Levinson-Durbin method
  432. Parameter:      A, B: allocated matrix where A is an autocorrelated matrix and
  433.                       B is derived from A
  434.  
  435.                 A, B must be the following forms:
  436.  
  437.                 |  a0   a1   a2  .. an-1 | |  x1   |    |  a1   |
  438.                 |  a1   a0   a1  .. an-2 | |  x2   |    |  a2   |
  439.                 |  a2   a1   a0  .. an-3 | |  x3   |  = |  ..   |
  440.                 |  ...                   | |  ..   |    |  ..   |
  441.                 |  an-1 an-2 ..  .. a0   | |  xn   |    |  an   |
  442.  
  443.                 where A is a symmetric Toeplitz matrix and B
  444.                 in the above format (related to A)
  445.  
  446. Return Value:   a newly allocated matrix X which is the solution of AX = B
  447.  
  448. Example:        this method only applies to certain A, B only.
  449.                 e.g.
  450.  
  451.                 A =  [1 3 9]        B = [3]
  452.                      [3 1 3]            [9]
  453.                      [9 3 1]            [12]  <- the last one is unrelated to A
  454.  
  455. See Also:       mat_SymToeplz
  456. -------------------------------------------------------------------------------
  457. Function:       MATRIX mat_SymToeplz (MATRIX R);
  458. Synopsis:       create a symmetric Toeplitz matrix from a
  459.                 Autocorrelation vector
  460. Parameter:      R: allocated matrix of dimension n x 1
  461. Return Value:   a newly allocated symmetrical Toeplitz matrix
  462. Example:
  463.                 e.g.
  464.  
  465.                 MATRIX  A, R;
  466.  
  467.                 R = mat_creat( 3, 1, UNDEFINED );
  468.                 ...
  469.                 A = mat_SymToeplz( R );
  470.  
  471.                 /*
  472.                 *  if
  473.                 *
  474.                 *  R =  [1 3 9]         A =  [1 3 9]
  475.                 *                            [3 1 3]
  476.                 *                            [9 3 1]
  477.                 *
  478.                 */
  479.  
  480. See Also:       mat_lsolve_durbin
  481. -------------------------------------------------------------------------------
  482.